/**
 * 日期时间处理工具类
 */
export default class DateTimeUtil {
    /**
     * 判断对象是否为日期对象
     */
    public static isDate(obj: object): boolean {
        return Object.prototype.toString.call(obj) === '[object Date]';
    }

    /**
     * 格式化日期时间, 格式化为 yyyy-MM-dd HH:mm:ss
     */
    public static formatDateTime(date: Date): string {
        const op: Intl.DateTimeFormatOptions = {
            year: 'numeric',
            month: '2-digit',
            day: '2-digit',
            hour: '2-digit',
            minute: '2-digit',
            second: '2-digit',
            hour12: false, // 24小时制
        }
        return date.toLocaleString("en-CA", op).replace(",", "");
    }

    /**
     * 格式化日期, 默认格式化为 yyyy-MM-dd
     */
    public static formatDate(date: Date): string {
        const op: Intl.DateTimeFormatOptions = {
            year: 'numeric',
            month: '2-digit',
            day: '2-digit',
        }
        return date.toLocaleString("en-CA", op);
    }

    /**
     * 格式化日期时间 YYYY-MM-DD HH:mm:ss.SSS
     */
    public static formatDateWithMillis(date: Date): string {
        const year = date.getFullYear();
        const month = String(date.getMonth() + 1).padStart(2, '0');
        const day = String(date.getDate()).padStart(2, '0');
        const hours = String(date.getHours()).padStart(2, '0');
        const minutes = String(date.getMinutes()).padStart(2, '0');
        const seconds = String(date.getSeconds()).padStart(2, '0');
        const milliseconds = String(date.getMilliseconds()).padStart(3, '0');
        return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}.${milliseconds}`;
    }

    /**
     * 获取当前日期时间,并格式化: YYYY-MM-DD HH:mm:ss.SSS
     */
    public static currentDateTimeWithMillis(): string{
        return DateTimeUtil.formatDateWithMillis(new Date())
    }

    /**
     * 获取当前日期,并格式化: YYYY-MM-DD
     */
    public static currentDate(): string {
        return DateTimeUtil.formatDate(new Date())
    }

    /**
     * 获取当前日期时间,并格式化: YYYY-MM-DD HH:mm:ss
     */
    public static currentDateTime(): string {
        return DateTimeUtil.formatDateTime(new Date())
    }

    /**
     * 根据当前传入的天树构建日期对象, 如果传递的日期不是1-31之间则反回null
     * @param year 当前需要构建的年份
     * @param month 当前需要构建的月份
     * @param day 当前需要构建的添加树
     */
    public static builderDateByYearMonthDay(year: string | number, month: string | number, day: string | number): Date | null {
        const _year = typeof year === "string" ? Number.parseInt(year) : year;
        const _month = typeof month === "string" ? Number.parseInt(month) : month;
        const _day = typeof day === "string" ? Number.parseInt(day) : day;
        if (_year > 2059 || _year < 2000) {
            return null;
        }
        if (_month < 1 || _month > 12) {
            return null;
        }
        if (_day < 1 || _day > 31) {
            return null;
        }
        const date = new Date(_year, _month, 0);
        if (_day > date.getDate()) {
            return null;
        }
        return new Date(_year, _month - 1, _day);

    }

    /**
     * 根据当前传入的天树构建日期对象, 如果传递的日期不是1-31之间则反回null
     * @param month 当前需要构建的月份
     * @param day 当前需要构建的添加树
     */
    public static builderDateByMonthDay(month: string | number, day: string | number): Date | null {
        const _month = typeof month === "string" ? Number.parseInt(month) : month;
        const _day = typeof day === "string" ? Number.parseInt(day) : day;
        if (_month < 1 || _month > 12) {
            return null;
        }
        if (_day < 1 || _day > 31) {
            return null;
        }
        const currentDate = new Date();
        // 获取当前年
        const year = currentDate.getFullYear();
        if (_day > new Date(year, _month, 0).getDate()) {
            return null;
        }
        return new Date(year, _month - 1, _day);
    }

    /**
     * 根据当前传入的天树构建日期对象, 如果传递的日期不是1-31之间则反回null
     * @param day 当前需要构建的添加树
     */
    public static builderDateByDay(day: string | number): Date | null {
        const currentDay = typeof day === "string" ? Number.parseInt(day) : day;
        if (currentDay < 1 || currentDay > 31) {
            return null;
        }
        const currentDate = new Date();
        // 获取当前年份和月份
        const year = currentDate.getFullYear();
        const month = currentDate.getMonth();
        if (currentDay > new Date(year, month + 1, 0).getDate()) {
            return null;
        }
        return new Date(year, month, currentDay);
    }
}